﻿using System.Collections.Generic;
using System.Drawing;
using PhiStore.Generics;
using PhiStore.Threading;

namespace PhiStore.ExampleCodes.PhiMovieStreamer
{
    public class MovieStreamer : Boostable
    {
        Channel<Bitmap> buffer;
        long loops;
        IEnumerable<Bitmap> movie;
        public MovieStreamer(Channel<Bitmap> buffer, IEnumerable<Bitmap> movie, long loops)
        {
            this.buffer = buffer;
            this.movie = movie;
            this.loops = loops;
            Booster.Boost(this);
        }

        public void Run()
        {
            try
            {
                for (long i = 0; i < loops; i++)
                {
                    foreach (Bitmap b in movie)
                    {
                        buffer.Send(b);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                buffer.Poison();
                if (movie is PhiArray<Bitmap>)
                {
                    (movie as PhiArray<Bitmap>).Disconnect();
                }
            }
        }
    }
}
